package br.com.fafica.sisape.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.fafica.sisape.conexao.ConnectionFactory;
import br.com.fafica.sisape.model.Alergia;

public class DAOAlergia implements InterfaceDAOAlergia {
	
	
	public void createAlergia(Alergia alergia, Integer cod) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "insert into Alergia "
				+ "(CodUsuario, Alergia, Reacoes) "
				+ "values (?,?,?)";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, cod);
			stmp.setString(2, alergia.getNome());
			stmp.setString(3, alergia.getReacao());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

	
	public ArrayList<Alergia> readAlergia(Integer cod) throws SQLException {
		Connection con = new ConnectionFactory().getConnection();
		String seleciona = " where CodUsuario="+Integer.toString(cod);
		String sql = "Select * from Alergia"+seleciona;

		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			ArrayList<Alergia> lista = new ArrayList<Alergia>();
			while (rs.next()) {
				Alergia alergia = new Alergia();
				alergia.setNome(rs.getString("Alergia"));
				alergia.setReacao(rs.getString("Reacoes"));
				alergia.setCod(rs.getInt("Cod"));
				lista.add(alergia);
			}
			rs.close();
			stmt.close();
			return lista;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
		return null;
	}

	
	public void updateAlergia(Alergia alergia) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "update Alergia set Alergia=?"
				+ " ,Reacoes=?"
				+ " where Cod=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setString(1, alergia.getNome());
			stmp.setString(2, alergia.getReacao());
			stmp.setInt(3, alergia.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
	}

	
	public void deleteAlergia(Alergia alergia) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "delete from Alergia where Cod=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, alergia.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Deletado com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

}
